//
//  FQMineHeaderCollectionViewCell.swift
//  SwiftProject
//
//  Created by 董駸 on 2022/8/25.
//

import UIKit

class FQMineHeaderCollectionViewCell: UICollectionViewCell {
    let headerImageV = UIImageView(imageString: "icon_headerImage")
    let titleLb = QMUILabel(font: .mSFont(16), textColor: .color51)
    let subTitleLb = QMUILabel(font: .rSFont(12), textColor: .color102)
    
    var buttonBlock : ((_ index : Int)->Void)?
    var headerBlock : (()->Void)?

    var model : FQMineHeaderModel! {
        didSet {
            if let model = BaseUserModel.unarchiver() {
                titleLb.text = model.phone.replaceStringWithAsterisk(from: 4, length: 4)
                subTitleLb.text = "您身边的借贷管家"
            } else {
                titleLb.text = "登录/注册"
                subTitleLb.text = "点击登录，使用宝银钱包"
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
//        clipsToBounds = true
        
        let imageV = UIImageView(imageString: "icon_mine_background")
        contentView.addSubview(imageV)
        
        imageV.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(287~)
        }
        
        headerImageV.isUserInteractionEnabled = true
        headerImageV.rx.tapGesture().when(.recognized).subscribe (onNext: { [unowned self] _ in
            if headerBlock != nil {
                headerBlock!()
            }
        }).disposed(by: rx.disposeBag)

        contentView.addSubview(headerImageV)
        headerImageV.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.size.equalTo(45)
            make.top.equalTo(NavigationContentTop)
        }
        
        titleLb.rx.tapGesture().when(.recognized).subscribe (onNext: { [unowned self] _ in
            if headerBlock != nil {
                headerBlock!()
            }
        }).disposed(by: rx.disposeBag)
        titleLb.isUserInteractionEnabled = true
        contentView.addSubview(titleLb)
        titleLb.snp.makeConstraints { make in
            make.left.equalTo(headerImageV.snp.right).offset(8)
            make.top.equalTo(headerImageV).offset(2)
        }
        
        subTitleLb.rx.tapGesture().when(.recognized).subscribe (onNext: { [unowned self] _ in
            if headerBlock != nil {
                headerBlock!()
            }
        }).disposed(by: rx.disposeBag)
        subTitleLb.isUserInteractionEnabled = true
        contentView.addSubview(subTitleLb)
        subTitleLb.snp.makeConstraints { make in
            make.left.equalTo(titleLb)
            make.bottom.equalTo(headerImageV).offset(-2)
        }
        
        let btnTitle = ["审核中", "待提现", "待还款", "拒件记录"]
        var btnArray : [QMUIButton] = []
        for (index, str) in btnTitle.enumerated() {
            let button = QMUIButton(title: str, titleColor: .color51, font: .rSFont(14))
            button.spacingBetweenImageAndTitle = 5
            button.imagePosition = .top
            button.setImage(UIImage(named: "icon_mine_button\(index + 1)"), for: .normal)
            button.addTarget(self, action: #selector(buttonClick(_:)), for: .touchUpInside)
            button.tag = 10 + index
            contentView.addSubview(button)
            btnArray.append(button)
        }
        btnArray.snp.makeConstraints { make in
            make.height.equalTo(80)
            make.top.equalTo(headerImageV.snp.bottom).offset(30)
        }
        btnArray.snp.distributeViewsAlong(axisType: .horizontal, fixedSpacing: 10, leadSpacing: 10, tailSpacing: 10)
    }
    
    @objc func buttonClick(_ button : QMUIButton) {
        if buttonBlock != nil {
            buttonBlock!(button.tag)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
